package com.k12.admim.controller.operator;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.k12.admim.model.entity.sys.SysRole;
import com.k12.admim.model.entity.sys.SysUser;
import com.k12.admim.model.vo.sys.SysRoleVo;
import com.k12.admim.model.vo.sys.SysUserVo;
import com.k12.admim.service.OperatorRoleService;
import com.k12.admim.utils.RequestContextUtil;
import com.k12.common.model.resp.R;
import com.k12.common.util.PageUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class OperatorRoleController {

    @Resource
    private OperatorRoleService operatorRoleService;


    @PostMapping("system/role")
    public R addRole(@RequestBody SysRoleVo sysRoleVo) {
        return operatorRoleService.insertRole(sysRoleVo);
    }

    @GetMapping("system/role/list")
    public R roleList(@RequestBody SysRoleVo sysRoleVo) {
        String tenantId = RequestContextUtil.getTenantId();
        if (StringUtils.isBlank(tenantId)){
            return R.failed("非法登录");
        }
        sysRoleVo.setTenantId(Long.valueOf(tenantId));
        List<SysRole> roleList = operatorRoleService.selectRoleList(sysRoleVo);
        PageUtil.PageResult<SysRole> result = PageUtil.paginate(roleList, sysRoleVo.getPageNum(), sysRoleVo.getPageSize());
        return R.success(result);
    }

    /**
     * 根据角色编号获取详细信息
     */
    @GetMapping(value = "system/role/{roleId}")
    public R getInfo(@PathVariable Long roleId) {
        String tenantId = RequestContextUtil.getTenantId();
        if (StringUtils.isBlank(tenantId)){
            return R.failed("非法登录");
        }
        operatorRoleService.checkRoleDataScope(roleId,tenantId);
        return R.success(operatorRoleService.selectRoleById(roleId,tenantId));
    }






    @DeleteMapping(value = "system/role/{roleIds}")
    public R  deleteRoleIds(@PathVariable Long[] roleIds) {
        String tenantId = RequestContextUtil.getTenantId();
        if (StringUtils.isBlank(tenantId)){
            return R.failed("非法登录");
        }
        String loginId = StpUtil.getLoginId().toString();
        return operatorRoleService.deleteRoleIds(tenantId,loginId,roleIds);
    }


    @PutMapping("system/role/dataScope")
    public R dataScope(@RequestBody SysRole role) {
        return operatorRoleService.authDataScope(role);
    }

    @GetMapping("system/role/authUser/allocatedList")
    public R allocatedList(SysUserVo userVo) {
        List<SysUser> list = operatorRoleService.allocatedList(userVo);
        PageUtil.PageResult<SysUser> result = PageUtil.paginate(list, userVo.getPageNum(), userVo.getPageSize());
        return R.success(result);
    }

    @PutMapping("system/role/authUser/selectAll")
    public R selectAuthUserAll(Long roleId, Long[] userIds)
    {
        String tenantId = RequestContextUtil.getTenantId();
        if(org.apache.commons.lang3.StringUtils.isBlank(tenantId)){
            return R.failed("非法登录");
        }
        operatorRoleService.checkRoleDataScope(roleId,tenantId);
        return R.success(operatorRoleService.insertAuthUsers(roleId, userIds,tenantId));
    }


}
